import { get, post } from '../contstants/fetch-request'
import { message } from 'antd/es';


export const getAssignments = (taskId: number) => {
    return (dispatch: Function) => {
        (async () => {
            const data = await get(`/api/v2/tasks/${taskId}/assignments`)
            dispatch({
                type: 'GET_ASSIGNMENTS',
                data
            })
        })()
    }
}
export const getAssignment = (assignmentId: number, callback?: Function) => {
    return (dispatch: Function) => {
        (async () => {
            const data = await get(`/api/v2/assignments/${assignmentId}/quizzes`)
            dispatch({
                type: 'GET_ASSIGNMENT',
                data
            })
            if (callback) {
                callback()
            }
        })()
    }
}
export const getReviewQuiz = (assignmentId: number, quizId: number) => {
    return (dispatch: Function) => {
        (async () => {
            const data = await get(`/api/v2/assignments/${assignmentId}/quizzes/${quizId}/review`)
            dispatch({
                type: 'GET_REVIEWQUIZ',
                data
            })
        })()
    }
}
export const submitAnswer = (userAnswer: string, taskId: number, assignmentId: number, quizId: number, type: string) => {

    const body = {
        userAnswer,
        type
    }

    return (dispatch: Function) => {
        (async () => {
            await post(`/api/v2/subjectQuizzes/tasks/${taskId}/assignments/${assignmentId}/quizzes/${quizId}/submission`, body)
            dispatch(getReviewQuiz(assignmentId, quizId))
            dispatch(getAssignment(assignmentId))
        })()
    }
}
export const submitBasicQuizAnswer = (answers: Array<any>, assignmentId: number) => {
    return (dispatch: Function) => {
        (async () => {
            await post(`/api/v2/assignments/${assignmentId}/answers`, answers)
            dispatch(getReviewQuiz(assignmentId, -1))
            dispatch(getAssignment(assignmentId))
        })()
    }
}
export const startAssignment = (taskId: number, assignmentId: number, userId: number, callback: Function) => {

    return () => {
        (async () => {
            await post(`/api/v2/tasks/assignments/users`, { taskId, assignmentId, userId })
            callback()
        })()
    }
}

export const submitSimpleCodingQuizAnswer = (data: any, callback: Function) => {
    return (dispatch: Function) => {
        (async () => {
            await post(`/api/simpleCodingQuizzes/submission`, data)
            dispatch(getRunningLogs(data.studentId, data.assignmentId, data.quizId, callback))
        })()
    }
}

export const submitSingleOnlineCoding = (data: any, loadLogs: Function) => {
    return () => {
        (async () => {
            const res = await post(`/api/v2/onlineLanguageQuizzes/submission`, data)
            loadLogs(res.data.id)
        })()
    }
}

export const getHomeworkQuizzes = (assignmentId: number) => {
    return (dispatch: Function) => {
        (async () => {
            const data = await get(`/api/v2/assignments/${assignmentId}/quizzes`)
            dispatch({
                type: 'REFRESH_HOMEWORK_QUIZZES',
                data
            })
        })()
    }
}

export const getRunningLogs = (userId: number, assignmentId: number, quizId: number, callback: Function) => {
    return (dispatch: Function) => {
        (async () => {
            const data = await get(`/api/simpleCodingQuizzes/users/${userId}/assignments/${assignmentId}/quizzes/${quizId}/submissions`)
            dispatch({
                type: 'REFRESH_SUBMISSION',
                data
            })
            if (callback) {
                callback()
            }
        })()
    }
}

export const getSingleOnlineCodingRunningLogs = (taskId: number, assignmentId: number, quizId: number, submissionId: number, getResult: Function) => {
    return () => {
        (async () => {
            const data = await get(`/api/v2/onlineLanguageQuizzes/submissions/${submissionId}/students/tasks/${taskId}/assignments/${assignmentId}/quizzes/${quizId}/runningLog`)
            if (getResult) {
                getResult(data)
            }
        })()
    }
}

export const getLogicQuiz = (quizId: number) => {
    return (dispatch: Function) => {
        (async () => {
            const data = await get(`/logicQuiz/api/tw-logic-quizzes/${quizId}`)
            dispatch({
                type: 'GET_TW_LOGIC_QUIZ',
                data: data
            })
        })()
    }
}

export const getLogicReviewQuiz = (assignmentId: number, quizId: number, callback: Function) => {
    return (dispatch: Function) => {
        (async () => {
            const data = await get(`/logicQuiz/logic-to-program/api/v2/assignments/${assignmentId}/quizzes/${quizId}/review`)
            dispatch({
                type: 'GET_LOGIC_REVIEWQUIZ',
                data: data
            })
            if (callback) {
                callback()
            }
        })()
    }
}

export const submitLogicReviewQuiz = (taskId: number, assignmentId: number, quizId: number) => {
    return (dispatch: Function) => {
        (async () => {
            await post(`/logicQuiz/logic-to-program/api/v2/review/logicQuizzes`, { taskId, assignmentId, quizId })
            dispatch(getLogicReviewQuiz(assignmentId, quizId, () => { }))
        })()
    }
}

export const submitLogicQuizItemAnswer = (answerData: any) => {
    return (dispatch: Function) => {
        (async () => {
            await post(`/logicQuiz/api/tw-logic-quiz-submission/answer`, answerData)
            dispatch({
                type: 'SUBMIT_LOGIC_QUIZ_ITEM_ANSWERS',
                data: answerData.answers
            })
            message.success('提交成功')
        })()
    }
}

export const getLogicQuizItem = (assignmentId: number, quizId: number, quizItemOrderNum: any, callback: Function) => {
    return (dispatch: Function) => {
        (async () => {
            const body = await get(`/logicQuiz/api/tw-logic-quiz-submission?assignmentId=${assignmentId}&quizId=${quizId}`)
            const { twlogicQuizItemAnswer, twexampleLogicQuizItemAnswer } = body
            body.twlogicQuizItemAnswer = twexampleLogicQuizItemAnswer.concat(twlogicQuizItemAnswer)
            dispatch({
                type: 'GET_TW_LOGIC_QUIZ_SUBMISSION',
                data: body
            })
            const idx = parseInt(quizItemOrderNum, 10) - 1
            const answerItem = body.twlogicQuizItemAnswer[idx]
            const quizResponse = await get(`/logicQuiz/api/v3/logicQuizzesItem/${answerItem.quizItemId}`)
            const data = Object.assign({}, quizResponse, answerItem)
            dispatch({
                type: 'GET_TW_LOGIC_QUIZ_ITEM',
                data: data
            })
            if (callback) {
                callback()
            }
        })()
    }
}

export const getLogicQuizSubmissionSummary = (assignmentId: number, quizId: number, callback: Function) => {
    return (dispatch: Function) => {
        (async () => {
            const data = await get(`/logicQuiz/api/tw-logic-quiz-submission/summary?assignmentId=${assignmentId}&quizId=${quizId}`)
            dispatch({
                type: 'GET_TW_LOGIC_QUIZ_SUBMISSION_SUMMARY',
                data: data
            })
            if (callback) {
                callback()
            }
        })()
    }
}

export const updateLogicQuizItemAnswer = (data: any) => ({
    type: 'UPDATE_LOGIC_QUIZ_ITEM_ANSWERS',
    data
})

